(
    function($) {
        $.fn.myScroll = function() {
            var $this = $(this),
                $leftBtn = $this.siblings('.btn-left'),
                $rightBtn = $this.siblings('.btn-right'),
                flag = true;
            var opts = {
                    boxNum: $this.find('img').length,
                    boxLen: 0,
                    showNum: 3,
                }
                // console.log(opts.boxLen())
            var doScroll = {
                init: function() {
                    opts.boxLen = opts.boxNum * 216;
                    $this.width(opts.boxLen);

                },
                ifLength: function() {

                    return;
                },
                // 左移动
                doleft: function() {
                    if (!flag) return false;

                    flag = false;
                    var leftNum = parseInt($this.css('margin-left')) - 216;

                    if (leftNum < -opts.boxLen + (216 * opts.showNum)) {
                        leftNum = 0;
                    }
                    $this.stop().animate({
                            'margin-left': leftNum,
                        },
                        300,
                        function() {
                            flag = true;
                        });

                },
                // 右移动
                doright: function() {
                    if (!flag) return false;
                    flag = false;

                    var leftNum = parseInt($this.css('margin-left')) + 216;
                    if (leftNum > 0) {
                        leftNum = -opts.boxLen + (216 * opts.showNum);

                    }
                    $this.stop().animate({
                            'margin-left': leftNum,
                        },
                        300,
                        function() {
                            flag = true;
                        });

                },
                doAuto: function() {
                    setInterval(
                        doScroll.doright, 3000)
                }
            };
            $leftBtn.click(function(event) {
                doScroll.doleft();
                return false;
            });
            $rightBtn.click(function(event) {
                doScroll.doright();
                return false;
            });
            doScroll.init();
        }

    }
)(window.jQuery || window.Zepto || window.$)
